﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OF.Security;
namespace OF.Security
{
    /// <summary>
    ///DefaultHandler 的摘要说明
    /// </summary>
    public class DefaultHandler : IHttpHandler
    {
        /// <summary>
        /// 是否重用
        /// </summary>
        public bool IsReusable
        {
            get { return true; }
        }

        /// <summary>
        /// 处理请求
        /// </summary>
        /// <param name="context">请求上下文</param>
        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request.QueryString["action"];
            if (action.Equals("AjaxLogin", StringComparison.InvariantCultureIgnoreCase))
            {
                //AjaxLogin(context.Request);
            }
            else if (action.Equals("LogOff", StringComparison.InvariantCultureIgnoreCase))
            {
                LogOff(context.Request);
            }
            else
            {
                context.Response.Write("非法访问！");
                context.Response.End();
            }

        }

  

        /// <summary>
        /// Ajax内容输出
        /// </summary>
        /// <param name="isValid"></param>
        /// <param name="parm"></param>
        private void AjaxResponse(bool isValid, string parm)
        {
            string js = "var loginResult={\"IsValid\":$0,\"parm\":\"$1\"};".Replace("$0", isValid ? "true" : "false").Replace("$1", parm.Replace("\"", "'"));
            HttpResponse Response = HttpContext.Current.Response;
            //js输出
            Response.Clear();
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AddHeader("Content-Type", "application/x-javascript");
            Response.Write(js);
        }

        /// <summary>
        /// 注销（HTML输出）
        /// </summary>
        /// <param name="Request"></param>
        public void LogOff(HttpRequest Request)
        {
            //System.Collections.Specialized.NameValueCollection QueryString = FillFromString(Request.RawUrl, true, System.Text.Encoding.Default);
            //Request.QueryString.Add(QueryString);
            IList<string> logOffUrls = DefaultAuthenticator.LogOffAllAPP();
            string ReturnUrl = Request.QueryString["RedirectUrl"] ?? System.Configuration.ConfigurationManager.AppSettings["DefaultHomeUrl"];
            HttpResponse Response = HttpContext.Current.Response;
            Response.Clear();
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AddHeader("Content-Type", "text/html");
            Response.Write(@"
<div>正在注销。。。</div>
    <div style='display:none'>");
            foreach (string item in logOffUrls)
            {
                Response.Write(string.Format("<img src='{0}' />", item));
            }
            Response.Write("</div>");
            Response.Write(@"
<script language='javascript'>
    window.onload = function () {
       window.location.href = '");
            Response.Write(ReturnUrl);
            Response.Write(@"';
    }
</script>");


        }



    }
}